package com.administrator.customviewtest.view.scrollview;

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.LinearLayout;

/**
 * Created by Administrator on 2017/6/6.
 */

public class MyScollLinearLayout extends LinearLayout {
    private static final String TAG = "MyScollLinearLayout";
    private int lastY;
    private int currentY;
    private int moveY;
    private int downY;
    private int i;
    private boolean isInterrupt = false;

    public MyScollLinearLayout(Context context) {
        super(context);
    }

    public MyScollLinearLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public MyScollLinearLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public MyScollLinearLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }


    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        Log.e(TAG, "move");
        int y = (int) ev.getY();
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                downY = y;
                break;
            case MotionEvent.ACTION_MOVE:
                Log.e(TAG, "move");
                currentY = y;
                if(currentY-downY>0){
                    moveY = currentY - downY;
                    i+=moveY;
                    layout(getLeft(), getTop() + moveY, getRight(), getBottom() + moveY);
                }
                break;
            case MotionEvent.ACTION_UP:
               layout(getLeft(), getTop()-i, getRight(), getBottom()-i);
               i=0;
                break;
        }
        return true;
    }
}
